import com.hyl.Application;
import com.hyl.DTO.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author hyl
 * @description
 * @create 2025-06-13 10:09
 */
@SpringBootTest(classes = Application.class)
public class UserMappertest {
    @Test
    public void getAllTest() {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        InputStream inputStream = null;
        try {
            //读取mybatis-config.xml文件内容到inputStream对象中
            inputStream = Resources.getResourceAsStream(resources);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(inputStream);
        //创建SqlSession实例
        SqlSession sqlSession = sqlMapper.openSession();

        String str = "com.hyl.mapper.UserMapper.addUser";
        User user = new User();
        user.setUserName("WangSir");
        user.setUserAge(22);
        user.setUserAddress("安徽理工大学");
        int rows = sqlSession.insert(str, user);
        System.out.println("添加用户结果：" + rows);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void updateUserTest() {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        InputStream inputStream = null;
        try {
            //读取mybatis-config.xml文件内容到inputStream对象中
            inputStream = Resources.getResourceAsStream(resources);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(inputStream);
        //创建SqlSession实例
        SqlSession sqlSession = sqlMapper.openSession();

        String str = "com.hyl.mapper.UserMapper.updateUser";
        User user = new User();
        user.setId(5);
        user.setUserName("LiSir");
        user.setUserAge(23);
        user.setUserAddress("安理工");
        int rows = sqlSession.update(str, user);
        System.out.println("更新用户结果：" + rows);
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void getUserTest() {
        //读取文件名
        String resources = "mybatis-config.xml";
        //创建流
        InputStream inputStream = null;
        try {
            //读取mybatis-config.xml文件内容到inputStream对象中
            inputStream = Resources.getResourceAsStream(resources);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        //初始化mybatis数据库,创建SqlSessionFactory类的实例
        SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(inputStream);
        //创建SqlSession实例
        SqlSession sqlSession = sqlMapper.openSession();

        // 输出数据库中的数据
        String str1 = "com.hyl.mapper.UserMapper.getUser";
        User myuser = sqlSession.selectOne(str1, 5);
        System.out.println("查询用户结果：" + myuser);
        //关闭session
        sqlSession.close();
    }
}
